<?php

/**
 * This is the model class for table "b2b_package".
 *
 * The followings are the available columns in table 'b2b_package':
 * @property string $id
 * @property string $name
 * @property integer $org_price
 * @property integer $sale_price
 * @property integer $suitable_gender
 * @property integer $status
 * @property string $description
 * @property string $create_at
 * @property string $update_at
 */
class B2bPackage extends CActiveRecord {
    const COMPANY_USER  = 0;
    const PUBLIC_USER  = 1;
    
    
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return B2bPackage the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'b2b_package';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('org_price, sale_price, suitable_gender,suitable_married', 'required'),
            array('org_price, sale_price, suitable_gender, status', 'numerical', 'integerOnly' => true),
            array('name', 'length', 'max' => 255),
            array('description', 'length', 'max' => 1000),
            array('create_at', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, name, osuitable_gender, suitable_married,status, description', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            "centers" => array(self::HAS_MANY, 'B2bPackageToCenter', array("package_id" => "id"))
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'name' => '套餐名',
            'org_price' => '原价格',
            'sale_price' => '销售价格',
            'suitable_gender' => '适合性别',
            'suitable_married' => '适合婚否',
            'status' => '状态',
            'description' => '描述',
            'create_at' => '创建日期',
            'update_at' => '更新时间',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('org_price', $this->org_price);
        $criteria->compare('sale_price', $this->sale_price);
        $criteria->compare('suitable_gender', $this->suitable_gender);
        $criteria->compare('status', $this->status);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('create_at', $this->create_at, true);
        $criteria->compare('update_at', $this->update_at, true);

        $ret = new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        	'pagination'=>array(
				'pageSize'=>20,
			),
        ));
        

        foreach($ret->getData() as $item){
        	$item->suitable_gender=$this->getSuitableGender($item->suitable_gender);
        	$item->suitable_married=$this->getSuitableMarried($item->suitable_married);
        }
        return $ret;
    }

    /*
     * 获取套餐列表
     * @param $status 订单状态
     * @return array
     */

    public function getPackageEachPage() {
        $criteria = new CDbCriteria();
        return new CActiveDataProvider("B2bPackage", array(
            'criteria' => $criteria,
            'pagination' => array('pageSize' => 2,
            ),
        ));
    }
    
    
	
}